package org.dromara.merchants.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.merchants.domain.bo.TaCategoryBo;
import org.dromara.merchants.domain.vo.TaCategoryVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.merchants.domain.vo.TaStoreVo;
import org.dromara.merchants.domain.bo.TaStoreBo;
import org.dromara.merchants.service.ITaStoreService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 店铺
 * 前端访问路由地址为:/merchants/store
 *
 * @author xiyin321
 * @date 2024-07-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/store")
public class TaStoreController extends BaseController {

    private final ITaStoreService taStoreService;

    /**
     * 查询店铺列表.
     */
    @SaCheckPermission("merchants:store:list")
    @GetMapping("/list")
    public TableDataInfo<TaStoreVo> list(TaStoreBo bo, PageQuery pageQuery) {
        return taStoreService.queryPageList(bo, pageQuery);
    }

    /**
     * app端查询店铺列表
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/appList")
    public TableDataInfo<TaStoreVo> appList(TaStoreBo bo, PageQuery pageQuery) {
        return taStoreService.queryAppList(bo, pageQuery);
    }
    /**
     * 查询店铺下拉列表
     */
    @GetMapping("option")
    public R<List<TaStoreVo>> option() {
        return R.ok(taStoreService.queryOptionList());
    }

    /**
     * 导出店铺列表
     */
    @SaCheckPermission("merchants:store:export")
    @Log(title = "店铺", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TaStoreBo bo, HttpServletResponse response) {
        List<TaStoreVo> list = taStoreService.queryList(bo);
        ExcelUtil.exportExcel(list, "店铺", TaStoreVo.class, response);
    }

    /**
     * 获取店铺详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<TaStoreVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(taStoreService.queryById(id));
    }

    /**
     * 新增店铺
     */
    @SaCheckPermission("merchants:store:add")
    @Log(title = "店铺", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TaStoreBo bo) {
        return toAjax(taStoreService.insertByBo(bo));
    }

    /**
     * 修改店铺
     */
    @SaCheckPermission("merchants:store:edit")
    @Log(title = "店铺", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TaStoreBo bo) {
        return toAjax(taStoreService.updateByBo(bo));
    }

    /**
     * 删除店铺
     *
     * @param ids 主键串
     */
    @SaCheckPermission("merchants:store:remove")
    @Log(title = "店铺", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(taStoreService.deleteWithValidByIds(List.of(ids), true));
    }
}
